﻿//行排序规则
function rowSort(a, b, thIndex, asc) {
    var textA = $(a.children[thIndex]).html();
    var textB = $(b.children[thIndex]).html();
    var digitA = parseFloat(textA);
    var digitB = parseFloat(textB);
    //字符串比较
    if (isNaN(digitA) || isNaN(digitB))
        return asc ? textA > textB : textA < textB;
    //数字比较
    return asc ? digitA > digitB : digitA < digitB;
};

//页面加载完成
$(document).ready(function () {
    //添加行排序筛选算法
    $(".order-filter").each(function () {
        //查找变量
        var tbody = $(this).find("tbody");
        var lines = $(tbody).children();
        var title = lines[0];
        var rows = new Array();
        for (var index = 1; index < lines.length; index++)
            rows.push(lines[index]);
        //遍历th
        $(title.children).each(function (thIndex, th) {
            //选项
            var options = new Array();
            $(rows).each(function (rowIndex, row) {
                var option = $(row.children[thIndex]).html();
                if ($.inArray(option, options) == -1)
                    options.push(option);
            });
            options.sort();
            //添加属性
            var html = '<span style="cursor:pointer;">' + $(th).html() + '</span><select><option>--</option>';
            $(options).each(function (optionIndex, option) {
                html += '<option>' + option + '</option>';
            });
            html += "</select>"
            $(th).html(html);
            //选择事件
            $(th).find("select").change(function () {
                //删除所有
                $(rows).each(function (rowIndex, row) {
                    $(row).remove();
                });
                //收集筛选值
                var selects = new Array();
                $(title.children).each(function (thIndex1, th1) {
                    selects.push($(th1).find("select").val());
                });
                //判断筛选值
                $(rows).each(function (rowIndex, row) {
                    var show = true;
                    $(row).children().each(function (cellIndex, cell) {
                        if (selects[cellIndex] != "--" && $(cell).html() != selects[cellIndex]) {
                            show = false;
                            return false; //break;
                        }
                    });
                    if (show)
                        $(tbody).after(row);
                });
            });
            //点击事件
            $(th).find("span").click(function () {
                //排序规则
                var order = $(th).attr("order");
                var asc = order == "asc";
                if (asc)
                    $(th).attr("order", "desc");
                else
                    $(th).attr("order", "asc");
                //排序 api:.sort()真的不靠谱，不信自己试
                //rows.sort(function (a, b) {
                //    return rowSort(a, b, thIndex, asc);
                //});
                //排序 冒泡排序...
                var temp;
                for (var index1 = rows.length - 1; index1 > 0; index1--) {
                    for (var index2 = 0; index2 < index1; index2++) {
                        if (rowSort(rows[index2], rows[index2 + 1], thIndex, asc)) {
                            temp = rows[index2 + 1];
                            rows[index2 + 1] = rows[index2];
                            rows[index2] = temp;
                        }
                    }
                }
                //筛选
                $(th).find("select").change();
            });
        });
    });
});